#include <iostream>

class Test
{
public:
    Test()
    {
        std::cout<<"构造函数"<<std::endl;
        p = new int;
    }

    Test(const Test& t)
    {
        std::cout<<"拷贝构造函数"<<std::endl;
        // p = new int(*t.p);
        p = new int;
        *p = *t.p;
    }

    Test& operator=(const Test& t)
    {
        std::cout << "赋值运算符重载" << std::endl;
        if (this != &t) {
            delete p;  // 释放原来的内存
            p = new int;  // 分配新的内存
            *p = *(t.p);  // 进行深拷贝
        }
        return *this;
    }
    
    Test& func();
    ~Test()
    {
        std::cout<<"析构函数"<<std::endl;
        delete p;
    }

private:
    int *p;
};

Test & Test::func()
{
    static Test t;//有名字，拷贝
    return t;
}

int main()
{
    // Test ();//匿名对象：生命周期只有一行代码
    // Test t = Test();

    Test t1;
    t1 = Test();

    std::cout<<"-----\n";
    // Test T2;
    return 0;
}

